import validate from "../../../utils/validate";
const app = getApp();
// pages/management/add-worker/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    realName: "",
    mobile: "",
    showPickerCrop: false,
    showPicker: false,
    farmerCrops: [],
    categoryList: [],
    result: [],
    newList: [],
    columns: ["精品", "次品", "统货"],
    activeCropId: null
  },

  // 品质选择
  onSelectQuality(e) {
    let activeCropId = e.currentTarget.dataset.id;
    this.setData({
      activeCropId,
      showPicker: true
    });
  },

  onPickerChange(event) {
    const { value, index } = event.detail;
    const { activeCropId, newList } = this.data;
    let newIndex = newList.findIndex((item) => item.cropId === activeCropId);
    newList[newIndex]["qualityType"] = index + 1;
    newList[newIndex]["qualityTypeName"] = value;
    this.setData({
      newList,
      activeCropId: null,
      showPicker: false
    });
  },

  /**
   * 农户添加完成回来
   */
  toFarmer() {
    this.submit();
  },

  submit() {
    if (!this.data.realName) {
      return wx.showToast({
        title: "请输入姓名",
        icon: "error",
        duration: 1000
      });
    }

    if (!this.data.mobile) {
      return wx.showToast({
        title: "请输入手机号码",
        icon: "error",
        duration: 1000
      });
    }

    if (!validate.mobile(this.data.mobile)) {
      return wx.showToast({
        title: "请输入正确号码",
        icon: "error",
        duration: 1000
      });
    }

    let farmerCrops = [];
    if (this.data.newList.length) {
      this.data.newList.map((item) => {
        farmerCrops.push({
          cropId: item.cropId,
          cropName: item.cropName,
          varietyId: item.varietyId,
          varietyName: item.varietyName,
          qualityType: item.qualityType || 1
        });
      });
    }

    wx.showLoading({
      title: "添加中..."
    });
    wx.$apis
      .addMarketUser({
        companyId: app.globalData.userInfo.companyData.companyId,
        realName: this.data.realName,
        mobile: this.data.mobile,
        marketRoleIds: [2],
        farmerCrops
      })
      .then((res) => {
        wx.navigateBack();
      });
  },

  // 输入框值检测
  onChange(e) {
    console.log(e);
  },

  noop() {},

  // 农产品选择弹框
  pickerCrops() {
    this.getCrops();
    this.setData({
      result: this.data.newList.map((item) => {
        return item.varietyName;
      }),
      showPickerCrop: true
    });
  },

  // 获取作物信息
  getCrops() {
    wx.showLoading({
      title: "获取中..."
    });
    wx.$apis.getCropCategoriesByCompanyId(app.globalData.userInfo.companyData.companyId).then((res) => {
      let categoryList = res.data.filter((item) => item.varietyId);
      this.setData({
        categoryList: categoryList.map((item) => {
          return {
            ...item,
            qualityType: 1, // 1-精品 2-次品 3-统货
            qualityTypeName: "精品",
            value: item.peelingType === 1 ? "一次除皮" : "二次除皮"
          };
        })
      });
    });
  },

  onCheckboxChange(event) {
    this.setData({
      result: event.detail
    });
  },

  toggle(event) {
    const { index } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },

  cancelcrops() {
    this.setData({
      showPickerCrop: false
    });
  },
  // 确认农产品
  surecrops() {
    let newList = [];
    this.data.result.map((kk) => {
      let obj = {};
      obj = this.data.categoryList.find((item) => {
        return item.varietyName == kk;
      });
      newList.push(obj);
    });
    this.setData({
      showPickerCrop: false,
      newList
    });
  }
});
